package com.news.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.news.model.Category;
import com.news.model.News;
import com.news.service.CategoryService;
import com.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.*;

@RequestMapping("/news")
@Controller
public class NewsController {
    @Autowired 
    private NewsService newsService;
    @Autowired
    private CategoryService categoryService;
    //手动添加
    @ResponseBody
    @RequestMapping("/insert")
    public String insertNews(News news){
        news.setId(UUID.randomUUID().toString().replace("-",""));
        news.setStatus("1");
        news.setTime(new Date());
        newsService.insertNews(news);
        return "redirect:/news/query/1/10";
    }
    //删除
    @ResponseBody
    @RequestMapping("/delete")
    public void deleteNews(News news){
        String status = news.getStatus();
        int i = Integer.parseInt(status)^1;
        news.setStatus(String.valueOf(i));
        newsService.uqdateNews(news);
    }  
    //查询单个

    // 查询全部
    @RequestMapping("/query/{pageNum}/{pageSize}")
    public String queryAll(Model model, @PathVariable("pageNum")int pageNum,@PathVariable("pageSize") int pageSize){
        List<News> news = newsService.queryAll(pageNum,pageSize);
        model.addAttribute("news", news);
        model.addAttribute("pageNum",pageNum);
        int pageCount = 0;
       int size = newsService.queryCount();
        if(size % pageSize == 0){
            pageCount = size / pageSize;
        }else{
            pageCount = size / pageSize + 1;
        }
        List<Category> categories = categoryService.queryAll(1, 10);
        model.addAttribute("categories",categories);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("link1","query");
        return "article-list";
    }
    @RequestMapping("/queryAll")
    @ResponseBody
    public void queryNews(String draw, String start, String length, HttpServletResponse response) throws JsonProcessingException {
        int pageSize = 10;
        int startRecord = 0;
        if(!"".equals(length) && null != length){
            pageSize = Integer.parseInt(length);
        }
        if(!"".equals(start) && null != start){
            startRecord = Integer.parseInt(start);
        }
        List<News> results = new ArrayList<News>();
        results = newsService.queryNews(pageSize,startRecord);
        Map<String,Object> map = new HashMap<String,Object> ();
        if(!"".equals(draw) && null != draw){
            map.put("draw",Integer.parseInt(draw));
        }
        map.put("draw",Integer.parseInt(draw));
        map.put("recordsTotal",results.size());
        map.put("recordsFiltered",results.size());
        map.put("data",results);
        ObjectMapper mapper = new ObjectMapper();
        String string = mapper.writeValueAsString(map);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }
    // 搜索
    @RequestMapping("/queryCategory/{pageNum}/{pageSize}")
    public String queryCategory(String id, Model model,String name,@PathVariable("pageNum")int pageNum,@PathVariable("pageSize") int pageSize) throws Exception{
        List<Category> categories = categoryService.queryAll(1, 10);
        model.addAttribute("categories",categories);
        if(name!=null && "0".equals(id)){
            List<News> news = newsService.queryOneNews(name);
            model.addAttribute("news", news);
            model.addAttribute("link1","news1");
            return "article-list";
        }else{
             List<News> news = newsService.queryCategory(id);
            List<News> news1 = newsService.queryCategory1(id, pageNum, pageSize);
            int pageCount = 0;
            int size = news.size();
            if(size % pageSize == 0){
                pageCount = size / pageSize;
            }else{
                pageCount = size / pageSize + 1;
            }
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("news", news1);
        model.addAttribute("link1","queryCategory");
        return "article-list";
        }
    }
    //查询一个
    @RequestMapping("/queryOne")
    public void queryOne(String id,Model model){
        System.out.println(id);
        News news = newsService.queryOne(id);
        String status = news.getStatus();
        if(status.equals("1")){
            news.setStatus(String.valueOf(0));
        }else news.setStatus(String.valueOf(1));
        newsService.uqdateNews(news);
    }
}
